/**
 * Copyright (c) 2020-2021 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 *
 */
/**
 * @file    turbox_timer.c
 * @brief   harmony bl platform timer adaptor
 * @details
 * @version 1.0
 * @author  ThunderSoft
 * @date    2020/12/3
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2020/12/3                ThunderSoft         Create it.
 *
 */
#include "riscv_hal.h"
#include "los_debug.h"
#include "soc.h"
#include "plic.h"
#include "mtimer.h"
#include "los_context.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

void __enable_irq(void)
{
  asm volatile ("csrsi mstatus, 8");
}

void __disable_irq(void)
{
  asm volatile ("csrci mstatus, 8");
}

VOID HalIrqDisable(UINT32 vector)
{
    //if (vector < 32) {
    if (0) {
        CLEAR_CSR(mie, 1 << vector);
    } else {
        PlicIrqDisable(vector);
    }
}

VOID HalIrqEnable(UINT32 vector)
{
    //if (vector < 32) {
    if (0) {
        SET_CSR(mie, 1 << vector);
    } else {
        PlicIrqEnable(vector);
    }
}

VOID HalSetLocalInterPri(UINT32 interPriNum, UINT16 prior)
{
    PlicIrqSetPrio(interPriNum, prior);
}

VOID HalGetSysCpuCycle(UINT32 *cntHi, UINT32 *cntLo)
{
    if ((cntHi == NULL) || (cntLo == NULL)) {
        return;
    }

    MTimerCpuCycle(cntHi, cntLo);
}

BOOL HalBackTraceFpCheck(UINT32 value)
{
    if (value >= (UINT32)(UINTPTR)(&__bss_end)) {
        return TRUE;
    }

    if ((value >= (UINT32)(UINTPTR)(&__start_and_irq_stack_top)) && (value < (UINT32)(UINTPTR)(&__except_stack_top))) {
        return TRUE;
    }

    return FALSE;
}

BOOL HalBackTraceRaCheck(UINT32 value)
{
    return TRUE;
}
/*
VOID HalClockInit(OS_TICK_HANDLER handler, UINT32 period)
{
    UINT32 ret;

    PlicIrqInit();
    HalIrqEnable(RISCV_MACH_EXT_IRQ);

    ret = MTimerTickInit(period);
    if (ret != LOS_OK) {
        PRINT_ERR("Creat Mtimer failed! ret : 0x%x \n", ret);
        return;
    }
}
*/
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
